package equipmentBusiness;

import impl.TestServerBusinessImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import models.HttpResponse;
import models.MyConfig;
import tools.Http;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangxianming on 2018/1/16.
 * 创建导入需要的excel并调用上传接口
 */
public class RunTest implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        JSONObject returnJson = JSON.parseObject("{\"apistatus\":0,\"result\":\"\",\"errorMsg\":\"\"}");
        String appPath = myConfig.getPropertiesConfig().getProperties().getProperty("appPath");
        String fileName = request.getParameter("fileName");
        String url = request.getParameter("url");
//        String url ="http://903.pmp.front.17shihui.com/p/api/rear/equipmentBase/importEquipmentListExcel?villageName=%E6%B5%A6%E4%B8%9C%E4%B8%80%E5%8F%B7&villageId=581701&channelId=11381";
        boolean isSuccess;
//        try {
//            url = URLDecoder.decode(url, "utf-8");
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        //创建测试文件 通过判断是否包含.xls, 这样就可以跳过“创建excel”这个步骤从而上传文件
        if (fileName.contains(".xls")){
            CreateTestFile createTestFile = new CreateTestFile(appPath);
            isSuccess = createTestFile.test(fileName);
        }else {
            isSuccess = true;
        }

        //登陆
        PmpGetToken pmpGetToken = new PmpGetToken();
        String token = pmpGetToken.getToken();

        //上传文件
        if (isSuccess){
            String fileType;
            try {
                fileType = getFileType(fileName);
            }catch (Exception e){
                returnJson.put("apistatus", 0);
                returnJson.put("errorMsg", e.getMessage());
                System.out.println(e.getMessage());
                return returnJson.toString();
            }
            Http http = new Http(appPath + "\\data\\equipment");
            JSONObject header = JSON.parseObject("{\"Content-Type\":\"multipart/form-data; boundary=\"}");
            header.put("Cookie", "PHPSESSID=dc6glrm82k462mrr4mutmlvkt7; token=" + token);
            String par = "{\"fileObject\":{\"nameType\":\"" + fileType + "\",\"path\":\"\",\"filename\":\"" + fileName + "\"},\"par\":{}}";

            HttpResponse httpResponse = http.sendRequest("POST", header, url, par);
            returnJson.put("result", httpResponse.getResponseResult());
            returnJson.put("apistatus", 1);
            return returnJson.toString();
        }else {
            returnJson.put("result", "System created test file Failed!!");
            return returnJson.toString();
        }
    }

    private String getFileType(String fileName) throws Exception{
        HashMap<String, String> type = new HashMap<String, String>();
        type.put(".xls", "filename");
        type.put(".xlx", "file");
        type.put(".jpg", "pic");
        type.put(".png", "pic");
        type.put(".jpeg", "pic");
        for (Map.Entry<String, String> me: type.entrySet()){
            if (fileName.contains(me.getKey()))
                return me.getValue();
        }
        throw new Exception("file type non-existent. append new type in the \"getFileType\" function.");
    }
}
